package com.hyyt.shopjsp.rating.buyersStrategyAndRecord.controller;

import com.hyyt.shopjsp.common.controller.BaseController;
import com.hyyt.shopjsp.rating.buyersStrategyAndRecord.pojo.RatingBuyersRecord;
import com.hyyt.shopjsp.rating.buyersStrategyAndRecord.service.IRatingBuyersRecordService;
import com.hyyt.shopjsp.util.common.Page;
import com.hyyt.shopjsp.util.common.ResultMsg;
import com.hyyt.shopjsp.util.common.Utils;
import com.hyyt.shopjsp.util.json.FastJsonUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/back/buyersRecord")
public class RatingBuyersRecordController extends BaseController {
	private Logger log = Logger.getLogger(RatingBuyersRecordController.class);
	private String basePath = "/back/rating/buyersStrategyAndRecord/";

	@Resource
	private IRatingBuyersRecordService ratingBuyersRecordService;//数据字典service接口

	/**
	 * 进入买家等级记录
	 *
	 * @return 返回到指定页面
	 */
	@RequestMapping("gotoBRCustomerPage")
	public String gotoBuyersRecordPage() {
		return basePath + "listBuyersRecord";
	}

	/**
	 * 查询所有买家等级记录
	 *
	 * @param loginName 登录名称
	 * @param phone 手机号
	 * @param page 分页
	 * @param response 结果集
	 */
	@RequestMapping(value = "listBuyersRecord", method = RequestMethod.POST)
	@ResponseBody
	public void listBuyersRecord(Page page, String loginName ,String phone ,HttpServletResponse response) {
		Map<String, Object> param = new HashMap<>();
		if (Utils.objectIsNotEmpty(loginName)) {//是否检索登录名称
			param.put("loginName", loginName.trim());
		}
		if (Utils.objectIsNotEmpty(phone)) {//是否检索手机号
			param.put("phone", phone.trim());
		}
		param.put("sortColumns", "registerDate desc");
		int totalRecordCount = ratingBuyersRecordService.countRatingBuyersRecordAndCustomer(param);
		page.setPageInfo(page.getPageSize(), totalRecordCount, page.getCurrentPage());
		List<Map<String, Object>> list = ratingBuyersRecordService.queryRatingBuyersRecordAndCustomer(param, page.getPageRecordBeginIndex(), page.getPageSize());
		param.clear();
		param.put("total", totalRecordCount);// total键 存放总记录数，必须的
		param.put("rows", list);// rows键 存放每页记录 list
		ResultMsg.create(param, response);  //把内容发送到页面
	}

	/**
	 * 根据等级记录id获得对象
	 *
	 * @param ratingRecordId
	 */
	@RequestMapping(value = "getBuyersRecordObject", method = RequestMethod.POST)
	@ResponseBody
	public ResultMsg getBuyersRecordObject(Integer ratingRecordId) {
		if (Utils.objectIsNotEmpty(ratingRecordId)) {
			Map<String, Object> param = new HashMap<>();
			param.put("ratingRecordId", ratingRecordId);
			List<Map<String, Object>> list = ratingBuyersRecordService.queryRatingBuyersRecordAndCustomerDetail(param);
			return ResultMsg.create(true, null, FastJsonUtils.toJSONString(list));
		}else {
			return ResultMsg.create(false);
		}
	}

	/**
	 * 跳转到会员等级升迁记录页面
	 *
	 * @param customerId 买家等级升迁记录ID
	 * @param param
	 * @return
	 */
	@RequestMapping("gotoCustomerLevelChangePage")
	public String gotoCustomerLevelChangePage(Integer customerId, Map<String, Object> param) {
		param.put("customerId", customerId);
		return basePath + "listBuyersLevelChange";
	}

	/**
	 * 根据会员id查询买家等级记录
	 *
	 * @param buyersLevel 买家等级
	 * @param customerId 会员Id
	 * @param page 分页
	 * @param response 结果集
	 */
	@RequestMapping(value = "listBuyersLevelChange", method = RequestMethod.POST)
	@ResponseBody
	public void listBuyersLevelChange(Page page, String buyersLevel ,Integer customerId ,HttpServletResponse response) {
		Map<String, Object> param = new HashMap<>();
		if (Utils.objectIsNotEmpty(customerId)) {//是否检索会员Id
			param.put("customerId", customerId);
		}
		if (Utils.objectIsNotEmpty(buyersLevel)) {//是否检索买家等级
			param.put("buyersLevel", buyersLevel);
		}
		param.put("sortColumns", "buyersLevel desc");
		int totalRecordCount = ratingBuyersRecordService.countRatingBuyersRecord(param);
		page.setPageInfo(page.getPageSize(), totalRecordCount, page.getCurrentPage());
		List<RatingBuyersRecord> list = ratingBuyersRecordService.queryRatingBuyersRecord(param, page.getPageRecordBeginIndex(), page.getPageSize());
		param.clear();
		param.put("total", totalRecordCount);// total键 存放总记录数，必须的
		param.put("rows", list);// rows键 存放每页记录 list
		ResultMsg.create(param, response);  //把内容发送到页面
	}

}